import 'dart:async';
import 'dart:convert';//模型转换用到
import 'package:convert/convert.dart';
import 'package:dio/dio.dart';
import 'package:flutter_reader/model/pay/pay_config_model.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_reader/tools/globals.dart' as globals;
import 'package:crypto/crypto.dart' as crypto;

 String BSAuthorization = globals.fullSessionID;
const BaseURL = 'http://appapi.98nice.cn/api';
const testURL = 'http://appapi.98nice.cn/api';

class PayDao {

  static Future<PayConfigModel> fetchConfig() async{
    final response = await http.get(testURL+'/config/pay'
        ,headers: {'BSAuthorization':globals.fullSessionID,'READING':'API'});
    if(response.statusCode == 200){
      //解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      final json = jsonDecode(utf8decoder.convert(response.bodyBytes));
      return PayConfigModel.fromJson(json);
    }
    else
    {
      throw Exception('加载payconfig接口失败');
    }
  }

  static Future confirmAliPay(String sessionID,String payCode) async{
    print('-----------支付宝支付-------------');
    Dio dio = Dio();

    final response = await dio.post(testURL+'/pay/commit/alipayapp',
        data: {
             'code':payCode
        },
        options: Options(
            contentType:Headers.formUrlEncodedContentType,
            headers: {
              'BSAuthorization':sessionID,
              'READING':'API',
            }));
    if(response.statusCode == 200)
    {
      return response.data;
    }
    else
    {
      print(response.statusCode);
      throw Exception('加载confirmpay接口失败');
    }
  }

  static Future confirmWechatPay(String sessionID,String payCode, int dateError) async{
    print('----------微信支付------------');
    int localDate = ((DateTime.now().millisecondsSinceEpoch)~/1000).toInt();
    int request_date = localDate - dateError;
    Map unsortMap = {
      'request_date': request_date,
      'code': payCode,
    };
    var sortMap = unsortMap.keys.toList()..sort();
    String MapCode = sortMap[0] + unsortMap[sortMap[0]].toString() + sortMap[1] + unsortMap[sortMap[1]].toString() + globals.appKey;
    print(MapCode);
    String autographCode = generateMd5(MapCode);
    Dio dio = Dio();
    print('request_date:${request_date}');
    print('paycode:${payCode}');
    print('sessionID:${sessionID}');
    final response = await dio.post(testURL + '/pay/commit/wechatapp',
    data: {
      'request_date': request_date,
      'code': payCode,
    },
    options: Options(
      contentType: Headers.formUrlEncodedContentType,
      headers: {
        'READING':'API',
        'BSAuthorization':sessionID
      }
    ));
    if(response.statusCode == 200)
    {
      return response.data;
    }
    else
    {
      print(response.statusCode);
      throw Exception('加载微信支付接口失败');
    }
  }
}

generateMd5(String data) {
  var content = new Utf8Encoder().convert(data);
  var md5 = crypto.md5;
  var digest = md5.convert(content);
  return hex.encode(digest.bytes);
}